package code1_100.code21_30;

/**
 * @author 韩迪
 * @date 2022/03/28 20:58
 **/
public class Code29 {
    public static int divide(int dividend, int divisor) {
        int flag = 0;
        int ret = 0;
        if (divisor==1){
            return dividend;
        }
        if (divisor==-1){
            if (dividend==Integer.MIN_VALUE)
                return Integer.MAX_VALUE;
            return -dividend;
        }
        if (dividend==Integer.MIN_VALUE){
            if (divisor==Integer.MIN_VALUE)
                return 1;
            if (divisor<0){
                divisor=-divisor;
                flag=1;
            }
            while (dividend<=0){
                dividend+=divisor;
                ret++;
            }
            ret--;
            if (flag==1)
                return ret;
            else
                return -ret;

        }




        if (dividend<0&&divisor<0){
            dividend=-dividend;
            divisor=-divisor;
            flag=1;
        }else if (dividend<0){
            dividend=-dividend;
        }else if(divisor<0){
            divisor = -divisor;
        }else {
            flag=1;
        }
        while (dividend>=0){
            dividend-=divisor;
            ret++;
        }
        ret--;
        if (flag==1)
            return ret;
        else
            return -ret;


    }


    public static void main(String[] args) {
        System.out.println(divide(-2147483648,2));
    }

}
